home *** CD-ROM | disk | FTP | other *** search
/ 45 Great Windows Utilities 6 / 45 Great Windows Utilities Volume 6 MOJO-405 (Mojo Software).iso / flute15 / sampdll.c < prev    next >
C/C++ Source or Header  |  1995-04-04  |  4KB  |  112 lines

  1. /****************************************************************************
  2.     File: SampDll.c
  3.     Contains a simple example DLL to illustrate how DLLs are called from
  4.     the CeSk programming lanaguage
  5. *******************************************************************************/
  6.  
  7. #include "windows.h"
  8. #include "windacts.h"
  9. #include "sampdll.h"
  10.  
  11. long     GetVarSize( union   varObj  FAR *);
  12. void    process1array(double FAR *,union varObj FAR *);
  13.  
  14. /* Main entry point for the library - all libraries have this entry point */
  15. /* Input: See Programmers Reference manual for details */
  16. int FAR PASCAL LibMain(hModule, wDataSeg, cbHeapSize, lpszCmdLine)
  17. HANDLE  hModule;
  18. WORD    wDataSeg;
  19. WORD    cbHeapSize;
  20. LPSTR   lpszCmdLine;
  21. {
  22.     return 1;
  23. }
  24.  
  25. /* Main exit routine for a DLL, tidies up any resources etc from DLL */
  26. /* See Programmers reference manual for details and parameters */
  27. int FAR PASCAL WEP (bSystemExit)
  28. int  bSystemExit;
  29. {
  30.     return(1);
  31. }
  32.  
  33. /**************************** Our DLL Code *******************************/
  34.  
  35. /* our example function for the DLL call, this function has been exported */
  36. /* in the SAMPDLL.DEF file */
  37. /* Input: hData - a handle to a global memory object containing a CeSk data */
  38. /*        array */
  39. /* Output: a HGLOBAL containing a CeSk double*/
  40. /* This example adds up all the positive numbers to give a total and */
  41. /* ignores any negative numbers, or non-numeric objects in the input array */
  42. HGLOBAL FAR PASCAL sumofpos(hData)
  43.         HGLOBAL hData;
  44.         {
  45.                 DataObj   FAR *op1;
  46.                 double  total;
  47.                 HGLOBAL result;
  48.  
  49.                 total=0e0;
  50.                 if (hData) {
  51.                         op1=PtrContent(hData);
  52.                         process1array(&total,op1);
  53.                         GlobalUnlock(hData);
  54.                 };
  55.  
  56.                 /* now package the result as a CeSk double */
  57.                 result=GlobalAlloc(GHND,sizeof(struct t_double));
  58.                 if (!result) return NULL;
  59.                 op1=(union varObj FAR *)GlobalLock(result);
  60.                 op1->tdouble.vtype=3;              /* type double */
  61.                 op1->tdouble.value=total;
  62.                 GlobalUnlock(result);
  63.                 return result;
  64.         };
  65.  
  66. /****************** subsidiary routines used by this DLL *****************/
  67.  
  68. /* process one CeSk item in the array, this is a reentrant routine */
  69. /* which processes an element, or an array of elements, adding the */
  70. /* positive numbers together and ignoring the rest */
  71. /* Input: ptotal - ptr to the current total */
  72. /*        op1 - ptr to the item to process */
  73. /* Output: adjusts the value pointed to by ptotal */
  74. void    process1array(ptotal,op1)
  75.         double  FAR *ptotal;
  76.         union varObj    FAR *op1;
  77.         {
  78.                 short   elements,i;
  79.                 double  val;
  80.  
  81.                 if (op1->tint.vtype==11) {
  82.                         /* process an array */
  83.                         elements=op1->tarray.elements;
  84.                         op1=(union varObj FAR *)(op1->tarray.value);
  85.                         for (i=0; i<elements; i++) {
  86.                                 process1array(ptotal,op1);
  87.                                 op1=(union varObj FAR *)(((char FAR *)(op1))+GetVarSize(op1));
  88.                         };
  89.                 } else {
  90.                         /* process a single element */
  91.                         switch (op1->tint.vtype) {
  92.                                 case 2: /* CeSk Float */
  93.                                         val=(double)(op1->tfloat.value);
  94.                                         break;
  95.                                 case 3: /* CeSk double */
  96.                                         val=op1->tdouble.value;
  97.                                         break;
  98.                                 case 1: /* CeSk integer */
  99.                                         val=(double)(op1->tint.value);
  100.                                         break;
  101.                                 case 9: /* CeSk Complex Number */
  102.                                         val=op1->tcomplex.realpart;
  103.                                         break;
  104.                                 default:
  105.                                         val=0e0;
  106.                                         break;
  107.                         };
  108.                         if (val>0e0) *ptotal+=val;
  109.                 };
  110.         };
  111.  
  112.